#include "MavenPackageWidget.h"
#include "MavenResult.h"

#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QComboBox>
#include <QPushButton>
#include <QPushButton>
#include <QListWidget>
#include <qboxlayout.h>
#include <qgroupbox.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qtabwidget.h>
#include <QTextEdit>
#include <qtextedit.h>


MavenPackageWidget::MavenPackageWidget(QWidget *parent) : QWidget(parent)
, m_groupId(new QLineEdit)
, m_artifactId(new QLineEdit)
, m_version(new QLineEdit)
, m_repositoryId(new QLineEdit)
, packageInfoBox(new QGroupBox(""))
, m_maven(new QTextEdit)
, m_gradle(new QTextEdit)
{
    QLabel *groupId = new QLabel("groupId:");
    QLabel *artifactId = new QLabel("artifactId:");
    QLabel *version = new QLabel("version:");
    QLabel *repositoryId = new QLabel("repositoryId");

    QPushButton *searchGroup = new QPushButton("搜索");
    QPushButton *searchArtifact = new QPushButton("搜索");
    QPushButton *searchVersion = new QPushButton("所有");

    connect(searchGroup, &QPushButton::clicked, [=]{
        emit emitSearchGroup(m_groupId->text());
    });
    connect(searchArtifact, &QPushButton::clicked, [=]{
        emit emitSearchArtifact(m_artifactId->text());
    });
    connect(searchVersion, &QPushButton::clicked, [=]{
        emit emitSearchVersion(m_groupId->text(),
                                m_artifactId->text(),
                                m_version->text());
    });

    QGridLayout *packageInfolayout = new QGridLayout();
    packageInfolayout->addWidget(groupId, 0, 0);
    packageInfolayout->addWidget(m_groupId, 0, 1);
    packageInfolayout->addWidget(searchGroup, 0, 2);

    packageInfolayout->addWidget(artifactId, 1, 0);
    packageInfolayout->addWidget(m_artifactId, 1, 1);
    packageInfolayout->addWidget(searchArtifact, 1, 2);

    packageInfolayout->addWidget(version, 2, 0);
    packageInfolayout->addWidget(m_version, 2, 1);
    packageInfolayout->addWidget(searchVersion, 2, 2);

    packageInfolayout->addWidget(repositoryId, 3, 0);
    packageInfolayout->addWidget(m_repositoryId, 3, 1, 1, 2);

    packageInfoBox->setLayout(packageInfolayout);

//
    QTabWidget *packageWidget = new QTabWidget;
    packageWidget->addTab(m_maven, "Maven");
    packageWidget->addTab(m_gradle, "Gradle");

    QVBoxLayout *dependencyBoxLayout = new QVBoxLayout();
    dependencyBoxLayout->addWidget(packageWidget);

    QGroupBox *dependencyBox = new QGroupBox("Dependency Information");
    dependencyBox->setLayout(dependencyBoxLayout);

    QVBoxLayout *packageLayout = new QVBoxLayout();
    packageLayout->addWidget(packageInfoBox);
    packageLayout->addWidget(dependencyBox);

    setLayout(packageLayout);
}

void MavenPackageWidget::setPackage(const MavenResult *package) {
    m_groupId->setText(package->g);
    m_artifactId->setText(package->a);
    m_version->setText(package->latestVersion);
    // 查询所有版本时没有该键将留空
    if (package->repositoryId.length() > 0)
        m_repositoryId->setText(package->repositoryId);
    
    packageInfoBox->setTitle(package->id);

    QString s_maven = 
                QString("<dependency>\n"
                        "    <groupId>%1</groupId>\n"
                        "    <artifactId>%2</artifactId>\n"
                        "    <version>%3</version>\n"
                        "</dependency>\n")
                        .arg(package->g)
                        .arg(package->a)
                        .arg(package->latestVersion);

     QString s_gradle = 
                QString("implementation '%1:%2:%3'\n")
                .arg(package->g)
                .arg(package->a)
                .arg(package->latestVersion);

    m_maven->setText(s_maven);
    m_gradle->setText(s_gradle);
}

// <!-- https://mvnrepository.com/artifact/junit/junit -->
// <dependency>
//     <groupId>junit</groupId>
//     <artifactId>junit</artifactId>
//     <version>4.13.2</version>
//     <scope>test</scope>
// </dependency>

// gradle short
// https://mvnrepository.com/artifact/junit/junit
// testImplementation 'junit:junit:4.13.2'


// {
//     "id": "junit:junit",
//     "g": "junit",                   groupId
//     "a": "junit",                   artifactId
//     "latestVersion": "4.13.2",      version
//     "repositoryId": "central",       Repositories
//     "p": "jar",
//     "timestamp": 1613233914000,
//     "versionCount": 32,
//     "text": [
//         "junit",
//         "junit",
//         "-javadoc.jar",
//         "-sources.jar",
//         ".jar",
//         ".pom"
//     ],
//     "ec": [
//         "-javadoc.jar",
//         "-sources.jar",
//         ".jar",
//         ".pom"
//     ]
// }


// https://search.maven.org/remotecontent?filepath=junit/junit/4.13.2/junit-4.13.2.pom
// https://search.maven.org/solrsearch/select?q=g:junit AND a:junit&core=gav&start=0&rows=32